"""
@author: 江同学呀
@file: collapse_search_results.py
@date: 2025/1/14 23:25
@desc:
    折叠搜索结果
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/collapse-search-results.html
"""
from typing import Dict, Union, TYPE_CHECKING

from espc.orm.model.base.base import _Base
if TYPE_CHECKING:
    from espc.orm.model.dsl.inner_hits_options import InnerHits
from espc.orm.model.mapping.field.base_field.base_field import _BaseField


class Collapse(_Base):
    """
    折叠搜索结果

    :param field:
    :param inner_hits:
    :param max_concurrent_group_searches:
    :param :
    """
    def __init__(
            self, field: Union[str, _BaseField], inner_hits: Union["InnerHits", Dict] = None,
            max_concurrent_group_searches: int = None
    ):
        super().__init__()
        self._field: Union[str, _BaseField] = field
        self._inner_hits: Union["InnerHits", Dict] = inner_hits
        self._max_concurrent_group_searches: int = max_concurrent_group_searches
        return

    def _build(self) -> Dict:
        _body: Dict = {
            "field": self._field._field_name
        }
        if self._inner_hits:
            _body["inner_hits"] = self._inner_hits if isinstance(self._inner_hits, Dict) else self._inner_hits._build()
        if self._max_concurrent_group_searches is not None:
            _body["max_concurrent_group_searches"] = self._max_concurrent_group_searches
        return _body

